package com.niiwoo.civet.account.service.check;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Client {

    private static void dateToMonth(String yearMonth){

        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(yearMonth));
            cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            String first = format.format(cal.getTime());
            System.out.println("first:"+first);

            Calendar ca = Calendar.getInstance();
            ca.setTime(sdf.parse(yearMonth));
            //ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            ca.add(Calendar.MONTH, 1);
            String last = format.format(ca.getTime());
            System.out.println("last:"+last);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        maxDateThread();
    }

    public static void main(String [] args){
        dateToMonth("2018-10");
    }

    public static void maxDateThread(){
        int countSigneds = 1000000;
        AtomicInteger index = new AtomicInteger(0);
        int pageSize = 100;
        ExecutorService executor = Executors.newWorkStealingPool(5);
        List<Callable<Boolean>> callables = new ArrayList<>();
        for(int i = 0 ; i <= 10 ; i ++ ){
            Callable callable = new Callable() {
                @Override
                public Boolean call() throws Exception {
                    while ( index.intValue() * pageSize  < countSigneds) {
                        for(int i = index.intValue() * pageSize; i < index.intValue() * pageSize + pageSize ; i++){
                            System.out.println(Thread.currentThread().getId() +"\t"+Thread.currentThread().getName() + "\t" + i);
                        }
                    }
                    return Boolean.TRUE;
                }
            };
            callables.add(callable);
        }
        try {
            executor.invokeAll(callables)
                    .stream()
                    .map(future -> {
                        try {
                            return future.get();
                        }
                        catch (Exception e) {
                            throw new IllegalStateException(e);
                        }
                    })
                    .forEach(System.out::println);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }




    public void thirdRunnable(){
        ExecutorService executor = Executors.newWorkStealingPool(1);
        List<Callable<String>> callables = Arrays.asList(
                () -> "task1",
                () -> "task2",
                () -> "task3");
        try {
            executor.invokeAll(callables)
                    .stream()
                    .map(future -> {
                        try {
                            return future.get();
                        }
                        catch (Exception e) {
                            throw new IllegalStateException(e);
                        }
                    })
                    .forEach(System.out::println);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 已测试完
     */
    public static void als(){
        Map<String,String> result = new HashMap<>();
        Callable c1 = new Callable() {
            @Override
            public Object call() throws Exception {
                for(int i =0 ; i<500000 ; i++){
                    System.out.println(Thread.currentThread().getId() +"\t"+Thread.currentThread().getName() + "\t" + i);
                }
                result.put("c1",Thread.currentThread().getId()+"\t"+Thread.currentThread().getName());
                return "c1";
            }
        };

        Callable c2 = new Callable() {
            @Override
            public Object call() throws Exception {
                for(int i =500000 ; i<500000+500000 ; i++){
                    System.out.println(Thread.currentThread().getId()+"========================" + "\t" + i);
                }
                result.put("c2",Thread.currentThread().getId()+"\t"+Thread.currentThread().getName());
                return "c2";
            }
        };

        Callable c3 = new Callable() {
            @Override
            public Object call() throws Exception {
                for(int i =500000 + 500000 ; i<500000+500000 + 500000 ; i++){
                    System.out.println(Thread.currentThread().getId() + "\t" + i);
                }
                result.put("c3",Thread.currentThread().getId()+"\t"+Thread.currentThread().getName());
                return "c3";
            }
        };

        ExecutorService executor = Executors.newWorkStealingPool(6);

        List<Callable<String>> callables = Arrays.asList(
                c1,
                c2,
                c3);

        try {
            executor.invokeAll(callables)
                    .stream()
                    .map(future -> {
                        try {
                            System.out.println("===============:"+future.get());
                            return future.get();
                        }
                        catch (Exception e) {
                            throw new IllegalStateException(e);
                        }
                    })
                    .forEach(System.out::println);
            System.out.println("++++++++++:"+result.toString());
            executor.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
